package cn.appkf.flappybird.actor;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;

import cn.appkf.flappybird.stage.TestStage;
import cn.appkf.flappybird.ui.MyMap;

/**
 * Created by Administrator on 2016/8/2.
 */
public class Skill extends Actor {



    private TextureRegion region;

    private float mapX;
    private float mapY;

    private float maxRange;

    private float speed=200f;

    private float nowRage=0f;

    private boolean isDown=false;

    private MyMap map;


    Rectangle badlogicRect;

    MyActor belong;


    TestStage stage;

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        batch.draw(
                region,
                getX(), getY(),
                getOriginX(), getOriginY(),
                getWidth(), getHeight(),
                getScaleX(), getScaleY(),
                getRotation()
        );
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        if(isDown()){
            return;
        }

        setX(mapX-map.getCam().position.x+map.getCam().viewportWidth/2);
        setY(mapY-map.getCam().position.y+map.getCam().viewportHeight/2);


        if(nowRage>maxRange){
            setDown(true);
            setVisible(false);
        }

        float move=speed*delta;
        mapX+=move;
        nowRage+=Math.abs(move);


    }

    public Skill( float x, float y,TestStage stage, MyMap map,MyActor actor) {
        super();

        this.stage=stage;
        this.belong=actor;

        this.region = new TextureRegion(stage.getManager().get(TestStage.A_FACE1,Texture.class));

        this.maxRange=500f;
        this.map=map;
        mapX=x;
        mapY=y;
        if(actor.getLast_redirect()==MyActor.STAY_LEFT){
            speed=-speed;
        }

        setWidth(region.getRegionWidth());
        setHeight(region.getRegionHeight());
        badlogicRect=new Rectangle();
    }


    public void checkAffect(MyActor actor){
        if(actor.equals(this.belong)){
            return;
        }
        badlogicRect.set(mapX,mapY,getWidth(),getHeight());
        Rectangle logoRect = new Rectangle();
        logoRect.set(actor.getMapX(),actor.getMapY(),actor.getWidth(),actor.getHeight());

        if (badlogicRect.overlaps(logoRect)) {
            actor.jump();
            actor.heart(-10);
        }
    }


    public boolean isDown() {
        return isDown;
    }

    public void setDown(boolean down) {
        isDown = down;
    }
}
